<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="品番编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuCode', validatorRules.skuCode]" placeholder="请输入品番编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuName']" placeholder="请输入品番名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="背番编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuBackCode']" placeholder="请输入背番编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="客户编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerNo']" placeholder="请输入客户编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerName']" placeholder="请输入客户名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="客户品番" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerSku']" placeholder="请输入客户品番"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番品牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuBrand']" placeholder="请输入品番品牌"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入库长度（cm）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['enterLength']" placeholder="请输入入库长度（cm）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入库宽度（cm）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['enterWidth']" placeholder="请输入入库宽度（cm）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入库高度（cm）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['enterHigh']" placeholder="请输入入库高度（cm）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入库体积（m³）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['enterVolume']" placeholder="请输入入库体积（m³）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入库规格数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['enterSpecification']" placeholder="请输入入库规格数" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="出库长度（cm）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['outLength']" placeholder="请输入出库长度（cm）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="出库宽度（cm）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['outWidth']" placeholder="请输入出库宽度（cm）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="出库高度（cm）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['outHigh']" placeholder="请输入出库高度（cm）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="出库体积（m³）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['outVolume']" placeholder="请输入出库体积（m³）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="出库规格数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['outSpecification']" placeholder="请输入出库规格数" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单个重量（g）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuWeight']" placeholder="请输入单个重量（g）"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="箱子体积（m³）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['boxVolume']" placeholder="请输入箱子体积（m³）" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BasicSkuInfoForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        skuCode: {
          rules: [{ required: true, message: '请输入品番编码!' }]
        }
      },
      url: {
        add: '/basicSkuInfo/basicSkuInfo/add',
        edit: '/basicSkuInfo/basicSkuInfo/edit',
        queryById: '/basicSkuInfo/basicSkuInfo/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'skuCode',
            'skuName',
            'skuBackCode',
            'customerNo',
            'customerName',
            'customerSku',
            'skuBrand',
            'enterLength',
            'enterWidth',
            'enterHigh',
            'enterVolume',
            'enterSpecification',
            'outLength',
            'outWidth',
            'outHigh',
            'outVolume',
            'outSpecification',
            'skuWeight',
            'boxVolume'
          )
        )
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then(res => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model.skuType = 2
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'skuType',
          'skuCode',
          'skuName',
          'skuBackCode',
          'customerNo',
          'customerName',
          'customerSku',
          'skuBrand',
          'enterLength',
          'enterWidth',
          'enterHigh',
          'enterVolume',
          'enterSpecification',
          'outLength',
          'outWidth',
          'outHigh',
          'outVolume',
          'outSpecification',
          'skuWeight',
          'boxVolume'
        )
      )
    }
  }
}
</script>
